package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.sys.SysFileInfoAddDto;
import com.dog.basics.bean.dto.admin.sys.SysFileInfoFromDto;
import com.dog.basics.bean.model.common.DogSysFileInfo;
import com.dog.basics.bean.vo.admin.sys.FileInfoVo;
import com.dog.basics.bean.vo.admin.sys.SysFileInfoVo;
import com.dog.basics.common.properties.FileProperties;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogSysFileInfoMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogSysFileInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件信息表 服务实现类
 * </p>
 *
 * @author guonh
 * @since 2020-05-22
 */
@Service
@AllArgsConstructor
public class DogSysFileInfoServiceImpl extends ServiceImpl<DogSysFileInfoMapper, DogSysFileInfo> implements IDogSysFileInfoService {
    private final FileProperties fileProperties;

    /**
     * @功能描述: 分页查询文件列表信息
     * @author He
     * @date 2020/4/16
     */
    @Override
    public IPage<SysFileInfoVo> getFilePageList(PageParam param, SysFileInfoFromDto sysFileInfoFromDto) {
        this.page(param, Wrappers.<DogSysFileInfo>lambdaQuery().eq(ObjectUtil.isNotNull(sysFileInfoFromDto.getFileStatus()), DogSysFileInfo::getFileStatus, sysFileInfoFromDto.getFileStatus()).like(StrUtil.isNotBlank(sysFileInfoFromDto.getName()), DogSysFileInfo::getName, sysFileInfoFromDto.getName()).orderByDesc(DogSysFileInfo::getCreateTime));
        // 数据格式化
        List<DogSysFileInfo> records = param.getRecords();
        param.setRecords(records.stream().map(e -> {
            SysFileInfoVo sysFileInfoVo = new SysFileInfoVo();
            BeanUtil.copyProperties(e, sysFileInfoVo);
            return sysFileInfoVo;
        }).collect(Collectors.toList()));
        return param;
    }

    /**
     * @Description: 增加文件
     * @Param: MessageAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/25  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(SysFileInfoAddDto sysFileInfoAddDto) {
        DogSysFileInfo dogSysFileInfo = new DogSysFileInfo();
        BeanUtil.copyProperties(sysFileInfoAddDto, dogSysFileInfo);
        dogSysFileInfo.setCreateTime(DateUtil.currentSeconds());
        dogSysFileInfo.setCreateBy(-1L);
        return this.save(dogSysFileInfo);
    }

    /**
     * @Description: 删除文件
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/25  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        // DigestUtils.md5Hex(new FileInputStream(path))
        DogSysFileInfo dogSysFileInfo = this.getById(id);
        Validator.validateNotNull(dogSysFileInfo, "数据不存在!");
        if (FileUtil.exist(FileUtil.file(fileProperties.getPathname() + "/" + dogSysFileInfo.getFileName()))) {
            boolean result = FileUtil.del(FileUtil.file(fileProperties.getPathname() + "/" + dogSysFileInfo.getFileName()));
            Validator.validateTrue(result, "删除文件失败");
        }
        return this.removeById(id);
    }

    /**
     * 获取fileInfo图片路径
     *
     * @param ids
     * @return guonh
     */
    @Override
    public List<FileInfoVo> getFileInfoUrl(List<Long> ids) {
        //判断为空则实例化一个空实体
        if (CollUtil.isEmpty(ids)) {
            return CollUtil.newArrayList();
        }
        List<FileInfoVo> fileInfoVos = this.list(Wrappers.<DogSysFileInfo>query().lambda()
                .in(DogSysFileInfo::getId, ids)).stream().map(info -> getFileMinIoUrl(info)).collect(Collectors.toList());
        return fileInfoVos;
    }

    /**
     * 获取MinIo路径根据桶与名字 (判断是否加锁)
     *
     * @param info
     * @return
     */
    public FileInfoVo getFileMinIoUrl(DogSysFileInfo info) {
        FileInfoVo fileInfoVo = new FileInfoVo();
        fileInfoVo.setFileId(info.getId());
        fileInfoVo.setFilePath(info.getFileName());
        return fileInfoVo;
    }
}
